/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jdesktop.wonderland.modules.hslutemplatecell.server;

import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.wonderland.common.cell.ClientCapabilities;
import org.jdesktop.wonderland.common.cell.messages.CellMessage;
import org.jdesktop.wonderland.common.cell.state.CellClientState;
import org.jdesktop.wonderland.common.cell.state.CellServerState;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellClientState;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellServerState;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellChangeMessage;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellGetShapeMessage;
import org.jdesktop.wonderland.server.cell.AbstractComponentMessageReceiver;
import org.jdesktop.wonderland.server.cell.CellMO;
import org.jdesktop.wonderland.server.cell.ChannelComponentMO;
import org.jdesktop.wonderland.server.comms.WonderlandClientID;
import org.jdesktop.wonderland.server.comms.WonderlandClientSender;

/**
 *
 * @author Dominik
 */
public class TemplateCellMO extends CellMO implements Observer {

    private static String shapeType = null;
    private AnimationCellMessageReceiver messageReceiver;
    private boolean settingState = false;

    public TemplateCellMO() {
        messageReceiver = null;
    }

    @Override
    protected String getClientCellClassName(WonderlandClientID clientID, ClientCapabilities capabilities) {
        return "org.jdesktop.wonderland.modules.hslutemplatecell.client.TemplateCell";
    }

    @Override
    public void setServerState(CellServerState state) {
        super.setServerState(state);
        this.setShapeType(((TemplateCellServerState) state).getShapeType());
    }

    @Override
    public CellServerState getServerState(CellServerState state) {
        if (state == null) {
            state = new TemplateCellServerState();
        }
        ((TemplateCellServerState) state).setShapeType(getShapeType());
        return super.getServerState(state);
    }

    @Override
    public CellClientState getClientState(CellClientState cellClientState, WonderlandClientID clientID, ClientCapabilities capabilities) {
        if (cellClientState == null) {
            cellClientState = new TemplateCellClientState();
        }
        ((TemplateCellClientState) cellClientState).setShapeType(getShapeType());
        return super.getClientState(cellClientState, clientID, capabilities);
    }

    @Override
    protected void setLive(boolean live) {
        super.setLive(live);
        ChannelComponentMO channel = getComponent(ChannelComponentMO.class);
        if (live == true) {

            if (messageReceiver == null) {
                messageReceiver = new AnimationCellMessageReceiver(this);
            }

            channel.addMessageReceiver(TemplateCellChangeMessage.class, (ChannelComponentMO.ComponentMessageReceiver) messageReceiver);
            channel.addMessageReceiver(TemplateCellGetShapeMessage.class, (ChannelComponentMO.ComponentMessageReceiver) messageReceiver);
        } else {
            channel.removeMessageReceiver(TemplateCellChangeMessage.class);
            channel.removeMessageReceiver(TemplateCellGetShapeMessage.class);
        }
    }

    /**
     * @return the shapeType
     */
    public String getShapeType() {
        return shapeType;
    }

    /**
     * @param shapeType the shapeType to set
     */
    public void setShapeType(String shapeType) {
        this.shapeType = shapeType;
    }

    @Override
    public void update(Observable o, Object arg) {
        settingState = true;
        this.setShapeType((String) arg);
        this.setServerState(new TemplateCellServerState(this.getShapeType()));
        settingState = false;
    }

    /**
     * @return the settingState
     */
    public boolean isSettingState() {
        return settingState;
    }

    private static class AnimationCellMessageReceiver extends AbstractComponentMessageReceiver {

        public AnimationCellMessageReceiver(TemplateCellMO cellMO) {
            super(cellMO);
            //this.seasonMsg = season;
        }

        public void messageReceived(WonderlandClientSender sender, WonderlandClientID clientID, CellMessage message) {
            TemplateCellMO cellMO = (TemplateCellMO) getCell();

            if (!cellMO.isSettingState()) {
                if (message instanceof TemplateCellChangeMessage) {
                    TemplateCellChangeMessage sccm = (TemplateCellChangeMessage) message;
                    cellMO.setShapeType(sccm.getShapeType());

                    if (clientID != null) {
                        cellMO.sendCellMessage(clientID, message);
                    }

                } else if (message instanceof TemplateCellGetShapeMessage) {
                    TemplateCellGetShapeMessage sccm = (TemplateCellGetShapeMessage) message;
                    sccm.setShapeType(cellMO.getShapeType());
                    cellMO.sendCellMessage(clientID, (CellMessage) sccm);
                }
            }
        }
    }
}
